﻿//----------------------------------------------------------------------------------------------------
// The Radial Menu is the primary point of action interaction for players
//----------------------------------------------------------------------------------------------------

package HG.Visual
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.utils.Timer;
	
	import HG.GameService;
	import HG.Managers.NodeManager;
	import HG.Managers.ActionManager;
	import HG.Managers.TurnManager;
	import HG.Managers.PlayerManager;
	import HG.Managers.ResourceManager;
	import HG.Managers.CameraManager;
	
	import HG.Visual.VisualNode;
	
	import HG.Actions.IAction;
	import HG.Managers.LevelManager;
	
	
	public class RadialMenu extends MovieClip
	{
		var m_buttons:Array;
		
		var m_nodeManager:NodeManager;
		var m_turnManager:TurnManager;
		var m_actionManager:ActionManager;
		var m_playerManager:PlayerManager;
		var m_resourceManager:ResourceManager;
		var m_levelManager:LevelManager;
		var m_cameraManager:CameraManager;
		
		var m_currentNode:Number;
		
		var m_enabled:Boolean;
		
		//----------------------------------------------------------------------------------------------------
		public function RadialMenu()
		{
			this.visible = false;
			m_buttons = new Array();
			
			m_nodeManager = GameService.GetInstance().GetNodeManager();
			m_turnManager = GameService.GetInstance().GetTurnManager();
			m_actionManager = GameService.GetInstance().GetActionManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			m_resourceManager = GameService.GetInstance().GetResourceManager();
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_cameraManager = GameService.GetInstance().GetCameraManager();
			
			this.addEventListener(MouseEvent.MOUSE_UP, OnMouseUp);
			this.addEventListener(MouseEvent.ROLL_OUT, OnMouseUp);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetEnabled(enable:Boolean)
		{
			m_enabled = enable;
		}
		
		//----------------------------------------------------------------------------------------------------
		private function OnMouseUp(me:MouseEvent):void
		{
			Hide();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function Hide()
		{
			var index:Number = 0;
			var listLength:Number = m_buttons.length;
			
			for (index = 0; index < listLength; ++index)
			{
				m_buttons[index].SetOpen(false, 0);
			}
			
			this.Zone.visible = false;
			
			m_cameraManager.LockCamera(false);
		}
		
		//----------------------------------------------------------------------------------------------------
		public function ButtonUp(me:MouseEvent)
		{
			var button:RadialButton = RadialButton(me.currentTarget.parent.parent);
			
			var playerNumber:Number = m_playerManager.GetCurrentPlayerID();
			var cost:Number = m_actionManager.GetActionCost(button.GetActionID());
			
			//if (m_resourceManager.CanPlayerAfford(playerNumber, cost))
			//{
				var action:IAction = m_actionManager.CreateAction(
					button.GetActionID(), 
					playerNumber,
					m_currentNode);
				
				m_turnManager.AddPendingAction(action);
				m_resourceManager.SpendResource(playerNumber, cost);
			//}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function ButtonHover(me:MouseEvent)
		{
			var button:RadialButton = RadialButton(me.currentTarget.parent.parent);
			button.OnHover(me);
			
			/*this.Desc.visible = true;
			this.Desc.gotoAndPlay('in');
			
			this.Desc.Content.TXT_Title.text = m_actionManager.GetActionName(button.GetActionID());
			this.Desc.Content.TXT_Desc.text = m_actionManager.GetActionDesc(button.GetActionID());*/
		}
		
		//----------------------------------------------------------------------------------------------------
		public function ButtonUnHover(me:MouseEvent)
		{
			var button:RadialButton = RadialButton(me.currentTarget.parent.parent);
			button.OnUnHover(me);
			
			this.Desc.gotoAndPlay('out');
		}
		
		//----------------------------------------------------------------------------------------------------
		public function SetTarget(node:VisualNode)
		{
			if (!m_enabled)
				return;
			
			m_cameraManager.LockCamera(true);
			
			this.x = node.x;
			this.y = node.y;
			this.visible = true;
			
			this.Zone.visible = true;
			
			m_currentNode = node.GetID();
			
			//Make sure we have a node adjacent to this one.
			var connections:Array = m_levelManager.GetAvailableVisualConnectionsTo(m_playerManager.GetCurrentPlayerID(), node.GetID());
			
			var actions:Array = m_nodeManager.GetNodeTypeActions(node.GetType());
		
			// filter the actions so only avaialble ones are shown-----------------------
			var filteredActions:Array = new Array()
			
			var index:Number = 0;
			var listLength:Number = actions.length;
			for (index = 0; index < listLength; ++index)
			{
				var actionID = actions[index];
				
				var available:Boolean = m_actionManager.CheckActionRequirements(actionID, node);
				
				if (connections.length <= 0)
					available = false;
				
				if (available)
				{
					filteredActions.push(actionID);
				}
			}
			
			// Push the actions to the menu-----------------------------------------------
			index = 0;
			listLength = filteredActions.length;
			
			var totalRotation:Number = listLength * 45;
			
			for (index = 0; index < listLength; ++index)
			{
				if (!m_buttons[index])
				{
					m_buttons[index] = new RadialButton();
					this.addChild(m_buttons[index]);
					
					m_buttons[index].Content.Zone.addEventListener(MouseEvent.MOUSE_UP, ButtonUp);
					m_buttons[index].Content.Zone.addEventListener(MouseEvent.MOUSE_OVER, ButtonHover);
					m_buttons[index].Content.Zone.addEventListener(MouseEvent.MOUSE_OUT, ButtonUnHover);
				}
				
				//m_buttons[index].visible = true;
				
				
				var rot:Number = (index * 45) - (totalRotation / 2);
				
				m_buttons[index].SetActionID(filteredActions[index]);
				m_buttons[index].SetRotation(rot);
				m_buttons[index].SetOpen(true, 50 + (index * 100));
			}
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}













